﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using System.Reflection;
using System.IO;

namespace intelliSys.NankaiUtilities.EventSynchronizer.EventProbing
{
    public partial class NKUEventProbingService : ServiceBase
    {

        public NKUEventProbingService(int probeInterval)
        {
            this.probeInterval = probeInterval;
        }

        int probeInterval = 60;
        Thread thread;
        public NKUEventProbingService()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            thread = new Thread(o =>
                {
                    while (true)
                    {
                        // EventLog.WriteEntry("Beginning Feature Probing");
                        try
                        {
                            if (NKUEventProber.DetectEvents())
                            {
                                //  EventLog.WriteEntry("Detected newly created events");
                                var assemblyPath = System.Reflection.Assembly.GetEntryAssembly().Location;
                                DirectoryInfo directory = new DirectoryInfo(assemblyPath);
                                //directory.Parent + "\\NKU Event Live Sync.appref-ms";
                                var appPath = directory.Parent.FullName + "\\NKU Event Live Sync.exe";
                                Process.Start(appPath);
                                // EventLog.WriteEntry("Launched Application");
                            }
                        }
                        catch
                        {
                            //EventLog.WriteEntry("Failed Detection Loop");
                        }
                        //Thread.Sleep(probeInterval * 1000 * 60);
                    }
                });
            thread.Start();
        }

        protected override void OnStop()
        {
            thread.Abort();
        }

        private void process1_Exited(object sender, EventArgs e)
        {

        }
    }
}
